﻿using Bandit.Helpers;
using ETC.DLEDLightingSystemPlus.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ETC.DLEDLightingSystemPlus.Helpers
{
   
    public class HoneyWell3320Helper:HoneyDeviceBase
    {
        public delegate void ScanBarcodeReceivedHandler(string Barcode);
        public event ScanBarcodeReceivedHandler OnScanBarcodeReceiveChanged;
        public void Close()
        {
            if (this.BanditSerialPort != null)
            {
                this.BanditSerialPort.Close();
                this.BanditSerialPort = null;
            }
        }

        public HoneyWell3320Helper(string portName) : base(portName)
        {




        }

        protected override void BanditSerialPort_OnReceived(object sender, OnReceivedArgs e)
        {

            _ResponseData = e.NormalData;// + this.BanditSerialPort.EndFlag;

            OnScanBarcodeReceiveChanged(_ResponseData);

            base.Set();

        }

        public override void Send(string cmd, bool isThread = false, int delayMS = 200)
        {
            //this.BanditSerialPort.EndFlag = "}";
            //this._ResponseState = false;

            this._ResponseData = string.Empty;

            LogHelper.Info(string.Format("SeriaPort Send -> {0}", cmd));

            if (isThread)
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(delayMS);

                    this.BanditSerialPort.Send(cmd, DataTypeBySerialPort.Hex, true);

                }).LogExceptions("Send - " + cmd);
            }
            else
            {
                this.BanditSerialPort.Send(cmd, DataTypeBySerialPort.Hex, true);


            }
        }

        #region 指令集

        /// <summary>
        /// HELP 信息指令:"HELP"
        /// </summary>
        /// <returns></returns>
        public string GetHelpInfoCommand()
        {
            string Cmd = "HELP";

            this.Send(Cmd);

            this.Wait(10);

            return _ResponseData;
        }

        public string OpenScanBarcode()
        {
            _ResponseData = string.Empty;

            string Cmd = "16 54 0D 6F 01";

            this.Send(Cmd);

            this.Wait(5);

            return _ResponseData;
        }
        public string CloseScanBarcode()
        {
            _ResponseData = string.Empty;

            string Cmd = "16 55 0D";

            this.Send(Cmd);

            this.Wait(5);

            return _ResponseData;
        }

        #endregion
    }
}
